export const handleDistributeFn = (
  distributeList,
  chooseItem,
  parameterObj,
  isAutoChangeOrder,
  orderList
) => {
  if (distributeList && distributeList.length && chooseItem) {
    const priorityDistributionList = [];

    const normalDistributionList = [];

    const saveByMaterialList = [];

    for (const item of distributeList) {
      const {
        decimalPlaces,
        delayTime = 1000,
        formulaList = [],
        distributeType,
        isSaveByMaterial = 0,
        orderKey,
        type,
        value,
        parameterCode = null,
        saveParameterCode,
        saveParameterName,
        isDistributeByAutoChangeOrder,
        parameterName = null,
      } = item;

      if (parameterCode) {
        let obj = {
          funId: parameterCode,
          isDistributeByAutoChangeOrder,
          parameterName,
          saveParameterName,
          saveParameterCode,
          isSaveByMaterial,
          distributeType,
        };

        if (isSaveByMaterial === 1 && !isNaN(Number(parameterObj[parameterCode]))) {
          obj['value'] = Number(parameterObj[parameterCode]);
        } else {
          if (type === 'value') {
            obj['value'] = value;
          } else if (type === 'orderKeyValue') {
            obj['value'] = chooseItem[orderKey];
          } else if (type === 'computedValue' && formulaList && formulaList.length) {
            const val = getComputedValueFn(formulaList, chooseItem);

            obj['value'] = val;
          } else if (type === 'orderKeyComputedValue') {
            const val = getOrderKeyComputedValueFn(formulaList, chooseItem, orderKey);

            obj['value'] = val;
          }

          if (!isNaN(Number(obj['value']))) {
            if (decimalPlaces) {
              // 小数位
              let zero = '';

              for (let i = 0; i < decimalPlaces; i++) {
                zero += '0';
              }

              const num = Number(1 + zero);

              if (!isNaN(num)) {
                obj['value'] = Number(obj['value'] / num);
              }
            }
          }
        }

        if (!isNaN(Number(obj['value']))) {
          const { producedNum, autoFlag } = chooseItem;

          if (parameterCode === 'productionQuantitySet' && !isNaN(Number(producedNum))) {
            if (isAutoChangeOrder === '1' && autoFlag === 1 && orderList.length) {
              let newObjValue = 0;

              orderList.forEach((oItem) => {
                if (oItem.autoFlag === 1) {
                  const { producedNum: oProducedNum, orderNum } = oItem;

                  newObjValue = newObjValue + (Number(orderNum) - Number(oProducedNum));
                }
              });

              console.log('自动换单下发产量设置', newObjValue);

              obj['value'] = newObjValue < 0 ? 0 : newObjValue;
            } else {
              obj['value'] =
                obj['value'] - Number(producedNum) < 0 ? 0 : obj['value'] - Number(producedNum);
            }
          }

          if (distributeType === 0) {
            priorityDistributionList.push(obj);
          } else if (distributeType === 1) {
            normalDistributionList.push(obj);
          } else if (distributeType === 2) {
            normalDistributionList.push({
              ...obj,
              delayTime,
              isDistributeByAutoChangeOrder,
            });
          }
        }

        if (isSaveByMaterial) {
          saveByMaterialList.push({
            parameterName,
            parameterCode,
            saveParameterName,
            saveParameterCode,
          });
        }
      }
    }

    return {
      priorityDistributionList,
      normalDistributionList,
      saveByMaterialList,
    };
  }
};

const getComputedValueFn = (formulaList, chooseItem) => {
  if (formulaList && formulaList.length) {
    const computedObj = formulaList[formulaList.length - 1];

    if (computedObj) {
      return getComputedValueResultFn(computedObj, chooseItem, formulaList);
    }
  }
};

const getComputedValueResultFn = (computedObj, chooseItem, formulaList) => {
  if (computedObj && chooseItem) {
    const { num1Val, num1ValType, num1OrderKey, num2Val, num2ValType, num2OrderKey, computedType } =
      computedObj;

    let num1 = null;

    let num2 = null;

    if (num1ValType === 'value') {
      num1 = num1Val;
    } else if (num1ValType === 'orderKeyValue') {
      num1 = chooseItem[num1OrderKey];
    } else if (num1ValType.includes('result')) {
      const i = Number(num1ValType.replace('result', ''));

      if (!isNaN(i) && formulaList[i - 1]) {
        num1 = getComputedValueResultFn(formulaList[i - 1], chooseItem, formulaList);

        if (!num1 && num1 !== 0) {
          num1 = null;
        }
      }
    }

    if (num2ValType === 'value') {
      num2 = num2Val;
    } else if (num2ValType === 'orderKeyValue') {
      num2 = chooseItem[num2OrderKey];
    } else if (num2ValType.includes('result')) {
      const i = Number(num2ValType.replace('result', ''));

      if (!isNaN(i) && formulaList[i]) {
        num2 = getComputedValueResultFn(formulaList[i], chooseItem, formulaList) || null;

        if (!num2 && num2 !== 0) {
          num2 = null;
        }
      }
    }

    if (num1 !== null && num2 !== null && computedType) {
      num1 = Number(num1);

      num2 = Number(num2);

      if (!isNaN(num1) && !isNaN(num2)) {
        if (computedType === 'plus') {
          return num1 + num2;
        } else if (computedType === 'reduce') {
          return num1 - num2;
        } else if (computedType === 'ride') {
          return num1 * num2;
        } else if (computedType === 'except') {
          return num1 / num2;
        }
      }
    }
  }
};

const getOrderKeyComputedValueFn = (formulaList, chooseItem, orderKey) => {
  if (formulaList && formulaList.length && chooseItem) {
    const newFormulaList = formulaList.filter((item) =>
      chooseItem[orderKey]
        ? item.orderKeyValue === chooseItem[orderKey]
        : [null, '', undefined].includes(item.orderKeyValue)
    );

    if (newFormulaList && newFormulaList.length) {
      const val = getComputedValueFn(newFormulaList, chooseItem);

      return val;
    }
  }
};
